import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../class/dialog/commont/diaButton.dart';
import '../../tool/assets.dart';
import 'commont/input.dart';

class ChangeNameView extends BaseDialog {
  String? name;
  void Function(String)? onChange;
  ChangeNameView({super.key});

  @override
  BaseDialogState createState() => _ChangeNameViewState();
}

class _ChangeNameViewState extends BaseDialogState<ChangeNameView> {
  final _textControlller = TextEditingController();
  bool _isValidator = true;
  @override
  void initState() {
    super.initState();
    _textControlller.text = widget.name ?? '';
    _textControlller.addListener(() {
      String text = _textControlller.text;
    });
  }

  @override
  Widget buildContent(BuildContext context) {
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constrains) {
      return Container(
        height: 160,
        width: constrains.maxWidth,
        child: Stack(
          children: [
            const Positioned(
              left: 0,
              right: 0,
              top: 0,
              height: 49,
              child: Center(
                child: Text(
                  'Please set your new name',
                  style: TextStyle(fontSize: 14, color: Colors.black),
                ),
              ),
            ),
            Positioned(
              left: 10,
              right: 10,
              top: 49,
              child: Container(height: 1.5, color: Colors.black),
            ),
            Positioned(
              left: 25,
              right: 74,
              top: 65,
              height: 50,
              child: Stack(
                children: [
                  const Positioned(
                    left: 0,
                    height: 20,
                    width: 50,
                    top: 5,
                    child: Text(
                      'Name:',
                      textAlign: TextAlign.right,
                      style: TextStyle(fontSize: 13, color: Colors.black),
                    ),
                  ),
                  Positioned(
                    left: 55,
                    right: 0,
                    height: 50,
                    child: InputView(
                        textAlign: TextAlign.center,
                        controller: _textControlller,
                        validator: (p0) {
                          String error = '';
                          if (_textControlller.text.isEmpty) {
                            error = 'The name can`t be null';
                          } else if (_textControlller.text.length > 6) {
                            error = 'Maximum 6 Characters';
                          }
                          _isValidator = error.isEmpty;

                          WidgetsBinding.instance.addPostFrameCallback((_) {
                            setState(() {});
                          });
                          if (_isValidator == false) {
                            return error;
                          }
                        }),
                  ),
                ],
              ),
            ),
            const Positioned(
              left: 10,
              right: 10,
              top: 90,
              height: 0,
              child: Text(
                'Maximum 6 Characters',
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 12, color: Colors.black),
              ),
            ),
            Positioned(
              left: 10,
              right: 10,
              top: 119,
              child: Container(height: 1.5, color: Colors.black),
            ),
            Positioned(
              left: 0,
              right: 0,
              top: 120,
              height: 40,
              child: Diabutton(
                title: 'OK',
                textColor: _isValidator ? Colors.black : Color_disEnable,
                enable: _isValidator,
                onClick: () {
                  if (_textControlller.text.isEmpty) {
                    EasyLoading.showInfo('The name can`t be null');
                    return;
                  } else if (_textControlller.text.length > 6) {
                    EasyLoading.showInfo('The name can`t be null');
                    return;
                  }
                  Navigator.pop(context);
                  if (widget.onChange != null) {
                    widget.onChange!(_textControlller.text);
                  }
                },
              ),
            ),
          ],
        ),
      );
    });
  }
}
